<template>
  <el-dialog
    title="新增部门"
    :visible.sync="isShowDialog"
    :before-close="onClose"
  >
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="deptForm" label-width="120px" :model="formData" :rules="rules">
      <el-form-item prop="name" label="部门名称">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item prop="code" label="部门编码">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item prop="manager" label="部门负责人">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @blur="managerBlur">
          <el-option v-for="item in employeesList" :key="item.id" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item prop="introduce" label="部门介绍">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click.native="addDepartments">确定</el-button>
        <el-button size="small" @click.native="onClose">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>

</template>

<script>
import { getDepartments, addDepartments, updateDepartments } from '@/api/departments'
import { getEmployeesimple } from '@/api/employees'
export default {
  props: {
    isShowDialog: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkNameRepeat = async(rule, value, callback) => {
      const { depts } = await getDepartments()
      let isRepeat
      if (this.formData.id) {
        isRepeat = depts.filter(item => item.pid === this.nodeData.pid).some(item => item.name === value && item.id !== this.formData.id)
      } else {
        isRepeat = depts.filter(item => item.pid === this.nodeData.id).some(item => item.name === value)
      }
      isRepeat ? callback(new Error('该子部门已经存在')) : callback()
    }
    const checkCodeRepeat = async(rule, value, callback) => {
      const { depts } = await getDepartments()
      let isRepeat
      if (this.formData.id) {
        isRepeat = depts.some(item => item.code === value && item.id !== this.formData.id)
      } else {
        isRepeat = depts.some(item => item.code === value)
      }
      isRepeat ? callback(new Error('全公司编码不能重复')) : callback()
    }
    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      // 定义校验规则
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: checkNameRepeat, trigger: 'blur' }
        ],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: checkCodeRepeat, trigger: 'blur' }
        ],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      },
      // 员工列表
      employeesList: []
    }
  },
  async created() {
    const employeesList = await getEmployeesimple()
    this.employeesList = employeesList
  },
  methods: {
    onClose() {
      this.$refs.deptForm.resetFields() // 重置校验字段
      this.formData = {}
      this.$emit('close')
    },
    // 为了部门负责人的校验
    managerBlur() {
      setTimeout(() => {
        this.$refs.deptForm.validateField('manager')
      }, 100)
    },
    // 确认
    addDepartments() {
      this.$refs.deptForm.validate(async(isOK) => {
        if (isOK) {
          if (this.formData.id) {
            await updateDepartments(this.formData)
            this.$emit('update')
            this.onClose()
            this.$message.success('编辑成功')
          } else {
            await addDepartments({ ...this.formData, pid: this.nodeData.id })
            this.$emit('add')
            this.onClose()
            this.$message.success('新增部门成功')
          }
        }
      })
    },
    // 点击编辑渲染表单
    getDepartmentsDetail(nodeData) {
      this.formData = { ...nodeData }
    }
  }
}
</script>

<style>

</style>
